package common.test;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

public class LongestConsecutive {

    public int longestConsecutive(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        Set<Integer> set = Arrays.stream(nums)
                .boxed()
                .collect(Collectors.toSet());
        int maxLength = 0;
        for (int num : nums) {
            if (!set.contains(num - 1)) {
                int tempLength = 1;
                while (set.contains(++num)) {
                    tempLength++;
                }
                maxLength = Math.max(tempLength, maxLength);
            }
        }

        return maxLength;
    }
}
